package fr.upstlse.glre.client.administration.utilisateurs;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.administration.component.Center;
import fr.upstlse.glre.client.administration.utilisateurs.action.CreateUserAction;
import fr.upstlse.glre.client.administration.utilisateurs.action.RoleSelectionAction;
import fr.upstlse.glre.client.espacededie.component.IUserModification;
import fr.upstlse.glre.client.widgets.fields.IUserFields;
import fr.upstlse.glre.client.widgets.fields.UserFields;

public class CreationPanel extends Composite implements IUserModification {
	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	private ContentPanel creationPanel;

	private UserFields userFields;
	private FormPanel formPanel;
	private IUserFields additionnalFields;
	
	private static CreationPanel instance;

	public CreationPanel() {
		CreationPanel.setInstance(this);
		
		createCreationPanel();
		createCreationForm();

		creationPanel.add(formPanel);
		initWidget(creationPanel);
	}

	private void createCreationPanel() {
		creationPanel = new ContentPanel();
		creationPanel.setAutoWidth(true);
		creationPanel.setWidth(500);
		creationPanel.setId(RfgConstante.ADMIN_UTILISATEURS_CREATION_PANEL_ID);
		creationPanel.setBorders(true);
		creationPanel.setCollapsible(true);

		// Draggable panel
		Draggable lDraggable = new Draggable(creationPanel, creationPanel
				.getHeader());
		lDraggable.setContainer(Center.getInstance());

		creationPanel
				.setHeading(RfgConstante.ADMIN_UTILISATEURS_CREATION_PANEL_HEADER_TEXT);
	}

	public void createCreationForm() {
		formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setAutoWidth(true);

		userFields = new UserFields(null, formPanel);
		userFields.getRole().addListener(Events.Select, new RoleSelectionAction(this));

		Button lSubmit = new Button(
				RfgConstante.ADMIN_UTILISATEURS_CREATION_SUBMIT);
		lSubmit.addSelectionListener(new CreateUserAction(userFields, this));
		formPanel.addButton(lSubmit);
	}

	public ContentPanel getCreationPanel() {
		return creationPanel;
	}

	public void setCreationPanel(ContentPanel creationPanel) {
		this.creationPanel = creationPanel;
	}

	public UserFields getUserFields() {
		return userFields;
	}

	public void setUserFields(UserFields userFields) {
		this.userFields = userFields;
	}
	
	public static CreationPanel getInstance() {
		if(instance == null) {
			instance = new CreationPanel();
		}
		
		return instance;
	}

	public static void setInstance(CreationPanel pCreationPanel) {
		CreationPanel.instance = pCreationPanel;
	}
	
	public IUserFields getAdditionalFields() {
		return additionnalFields;
	}

	public FormPanel getFormPanel() {
		return formPanel;
	}

	public void setAdditionalFields(IUserFields pAdditionalFields) {
		additionnalFields = pAdditionalFields;

	}

	public void setFormPanel(FormPanel pFormPanel) {
		formPanel = pFormPanel;

	}
}
